export default {
    namespaced:true,
    state:{
        list:[ //商品列表
            {id:1,title:'霸王洗发水',price:100,power:'防脱',showcart:false},
            {id:2,title:'清扬洗发水',price:200,power:'去屑',showcart:false},
            {id:3,title:'飘柔洗发水',price:300,power:'柔顺',showcart:false}
        ],
        shopcarList:[] //购物车列表
    },
    getters:{
        totalMoney(state){
            //总金额=遍历购物车列表，累加   单价 *  数量
            let total=0;
            state.shopcarList.forEach(item=>{
                total +=item.price * item.count
            })
            return total;
        }
    },
    mutations:{
        //加入购物车
        add(state,item){
            state.shopcarList.push({
                id:item.id,
                title:item.title,
                price:item.price,
                count:1
            })
            item.showcart = true;
        },
        //删除
        del(state,id){
            console.log(id)
            //根据ID去商品列表里寻找对应的index，然后修改状态
            let index = state.list.findIndex(item=>item.id==id)
            state.list[index].showcart=false;
            //根据ID去购物车列表寻找对应的index，然后删除
            let index2 = state.shopcarList.findIndex(item=>item.id==id)
            state.shopcarList.splice(index2,1)
        },
        //清空
        clear(state){
            state.list.map(item=>item.showcart=false)
            state.shopcarList = []
        }
    },
    actions:{}
}